﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint;

namespace MFD.SharePoint.TaskNotification
{
    public class TaskNotificationJob:SPJobDefinition
    {
       
      //// The settings for the job are stored on the Job itself

      //  [Persisted]
      //  private bool _enableTaskAboutToStartNotification = true;

      //  public bool EnableTaskAboutToStartNotification
      //  {
      //      get { return _enableTaskAboutToStartNotification; }
      //      set { _enableTaskAboutToStartNotification = value; }
      //  }

      //  [Persisted]
      //  private string _taskAboutToStartSubject;

      //  public string TaskAboutToStartSubject
      //  {
      //      get { return _taskAboutToStartSubject; }
      //      set { _taskAboutToStartSubject = value; }
      //  }

      //  [Persisted]
      //  private string _taskAboutToStartBody;

      //  public string TaskAboutToStartBody
      //  {
      //      get { return _taskAboutToStartBody; }
      //      set { _taskAboutToStartBody = value; }
      //  }

      //  [Persisted]
      //  private int _daysBeforeStartDate = 1;

      //  public int DaysBeforeStartDate
      //  {
      //      get { return _daysBeforeStartDate; }
      //      set { _daysBeforeStartDate = value; }
      //  }


      //  [Persisted]
      //  private bool _enableTaskNotStartedNotification = true;

      //  public bool EnableTaskNotStartedNotification
      //  {
      //      get { return _enableTaskNotStartedNotification; }
      //      set { _enableTaskNotStartedNotification = value; }
      //  }

      //  [Persisted]
      //  private string _taskNotStartedSubject;

      //  public string TaskNotStartedSubject
      //  {
      //      get { return _taskNotStartedSubject; }
      //      set { _taskNotStartedSubject = value; }
      //  }

      //  [Persisted]
      //  private string _taskNotStartedBody;

      //  public string TaskNotStartedBody
      //  {
      //      get { return _taskNotStartedBody; }
      //      set { _taskNotStartedBody = value; }
      //  }

      //  [Persisted]
      //  private bool _enableTaskDueNotification = true;

      //  public bool EnableTaskDueNotification
      //  {
      //      get { return _enableTaskDueNotification; }
      //      set { _enableTaskDueNotification = value; }
      //  }

      //  [Persisted]
      //  private string _taskDueSubject;

      //  public string TaskDueSubject
      //  {
      //      get { return _taskDueSubject; }
      //      set { _taskDueSubject = value; }
      //  }

      //  [Persisted]
      //  private string _taskDueBody;

      //  public string TaskDueBody
      //  {
      //      get { return _taskDueBody; }
      //      set { _taskDueBody = value; }
      //  }

      //  [Persisted]
      //  private int _daysBeforeDueDate = 1;

      //  public int DaysBeforeDueDate
      //  {
      //      get { return _daysBeforeDueDate; }
      //      set { _daysBeforeDueDate = value; }
      //  }


      //  [Persisted]
      //  private bool _enableTaskOverDueNotification = true;

      //  public bool EnableTaskOverDueNotification
      //  {
      //      get { return _enableTaskOverDueNotification; }
      //      set { _enableTaskOverDueNotification = value; }
      //  }

      //  [Persisted]
      //  private string _taskOverDueSubject;

      //  public string TaskOverDueSubject
      //  {
      //      get { return _taskOverDueSubject; }
      //      set { _taskOverDueSubject = value; }
      //  }

      //  [Persisted]
      //  private string _taskOverDueBody;

      //  public string TaskOverDueBody
      //  {
      //      get { return _taskOverDueBody; }
      //      set { _taskOverDueBody = value; }
      //  }


      //  [Persisted]
      //  private string _webURL;

      //  public string WebURL
      //  {
      //      get { return _webURL; }
      //      set { _webURL = value; }
      //  }

      //  [Persisted]
      //  private Guid _listID;

      //  public Guid ListID
      //  {
      //      get { return _listID; }
      //      set { _listID = value; }
      //  }



        
        public TaskNotificationJob() : base(){  }
      
        public TaskNotificationJob (string jobName, SPService service, SPServer server, SPJobLockType targetType)
           : base (jobName, service, server, targetType) {    }

        public TaskNotificationJob(string jobName, SPWebApplication webApplication)
          : base (jobName, webApplication, null, SPJobLockType.Job) 
        {
         // this.Title = "Task Notification";
        }



        public override void Execute(Guid targetInstanceId)
        {
            Guid featureID = new Guid(TaskNotification.NOTIFICATION_SITE_FEATURE_GUID);
            //we check all the sites for the notification feature to be enabled.
            foreach (SPSite site in this.WebApplication.Sites)
            {
                SPFeature feature = site.Features[featureID];
                if (feature != null) //the feature is activated
                {
                    foreach (SPFeatureProperty prop in feature.Properties)
                    {
                        
                            string listID = prop.Name;
                            TaskNotificationConfig cfg = Utils.Deserialize<TaskNotificationConfig>(prop.Value);
                            if (cfg.NotificationSchedule < DateTime.Now.AddMinutes(3)) //check if we need to notify
                            {
                                //update the notification Schedule date part to mark the fact that we notified
                                cfg.NotificationSchedule = new DateTime(DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day, cfg.NotificationSchedule.Hour, cfg.NotificationSchedule.Minute, 0).AddDays(1);

                                try
                                {

                                    //Get the SPWeb
                                    using (SPWeb web = site.OpenWeb(cfg.WebURL))
                                    {
                                        //Get the SPList
                                        SPList taskList = web.Lists[cfg.ListID];

                                        List<int> sentEmails = new List<int>();
                                        if (cfg.EnableTaskOverDueNotification)
                                            TaskNotification.SendTaskNotification(taskList, string.Format(TaskNotification.TaskOverDueQuery, cfg.StatusColumnName, cfg.OverdueStatusChoice), string.Format(TaskNotification.QueryViewFields, cfg.StatusColumnName), cfg.TaskOverDueBody, cfg.TaskOverDueSubject, ref sentEmails);

                                        if (cfg.EnableTaskDueNotification)
                                            TaskNotification.SendTaskNotification(taskList, string.Format(TaskNotification.TaskDueQuery, cfg.StatusColumnName, cfg.DueStatusChoice, cfg.DaysBeforeDueDate), string.Format(TaskNotification.QueryViewFields, cfg.StatusColumnName), cfg.TaskDueBody, cfg.TaskDueSubject, ref sentEmails);


                                        if (cfg.EnableTaskNotStartedNotification)
                                            TaskNotification.SendTaskNotification(taskList, string.Format(TaskNotification.TaskNotStartedQuery, cfg.StatusColumnName, cfg.NotStartedStatusChoice), string.Format(TaskNotification.QueryViewFields, cfg.StatusColumnName), cfg.TaskNotStartedBody, cfg.TaskNotStartedSubject, ref sentEmails);

                                        if (cfg.EnableTaskAboutToStartNotification)
                                            TaskNotification.SendTaskNotification(taskList, string.Format(TaskNotification.TaskAboutToStartQuery, cfg.StatusColumnName, cfg.AboutToStartStatusChoice, cfg.DaysBeforeStartDate), string.Format(TaskNotification.QueryViewFields, cfg.StatusColumnName), cfg.TaskAboutToStartBody, cfg.TaskAboutToStartSubject, ref sentEmails);

                                    }

                                    //we need to update the notification config
                                    prop.Value = Utils.Serialize(cfg);

                                }
                                catch (Exception ex)
                                {
                                    System.Diagnostics.Trace.TraceError("Error Processing List ID {0} on {1}:/n/r{2}", prop.Name, cfg.WebURL, ex);
                                }

                            }
                        

                    }
                    //we update the feature properties in the database
                    feature.Properties.Update();
                }


            }
        }



   

    }
}
